package com.angel.web.app;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.base.JsonEntity;
import com.angel.core.util.JsonUtil;
import com.angel.health.Archives;
import com.angel.health.BuzAssessment;
import com.angel.provider.health.BuzAssessmentProvider;
import com.angel.provider.health.BuzCprProvider;
import com.angel.provider.health.BuzExaminationProvider;
import com.angel.provider.health.BuzMedicineRecordProvider;
import com.github.pagehelper.StringUtil;

/**
 * @author 作者姓名 cheng
 * @version 创建时间：2016年9月24日 下午2:01:17 类说明 app健康档案
 */
@Controller
@RequestMapping("app/archives")
public class AppArchivesController {
	@Autowired
	private BuzCprProvider bcProvider;
	@Autowired
	private BuzExaminationProvider exaProvider;
	@Autowired
	private BuzMedicineRecordProvider bmrProvider;
	@Autowired
	private BuzAssessmentProvider baProvider;

	@ResponseBody
	@RequestMapping(value = "index", produces = "application/json; charset=utf-8")
	public String index(@ModelAttribute("customerId") String customerId) {
		if (StringUtil.isEmpty(customerId)) {
			return JsonUtil.toJson(new JsonEntity("-1", "参数不正确！"));
		}
		List<Archives> list = new ArrayList<Archives>();
		List<Archives> bcList = bcProvider.findCprForApp(customerId);
		list.addAll(bcList);
		List<Archives> beList = exaProvider.findExamForApp(customerId);
		list.addAll(beList);
		List<Archives> bmrList = bmrProvider.findMRForApp(customerId);
		list.addAll(bmrList);

		Collections.sort(list, new Comparator<Archives>() {
			public int compare(Archives arg0, Archives arg1) {
				String time1 = StringUtil.isEmpty(arg0.getRecordTime())?"":arg0.getRecordTime();
				String time2 = StringUtil.isEmpty(arg1.getRecordTime())?"":arg1.getRecordTime();
				return time2.compareTo(time1);
			}
		});
		return JsonUtil.toJson(new JsonEntity("200", list));
	}
	
	@ResponseBody
	@RequestMapping(value = "appindex", produces = "application/json; charset=utf-8")
	public String appindex(@ModelAttribute("customerId") String customerId) {
		if (StringUtil.isEmpty(customerId)) {
			return JsonUtil.toJson(new JsonEntity("-1", "参数不正确！"));
		}
		List<Archives> list = new ArrayList<Archives>();
		List<Archives> bcList = bcProvider.findCprForApp(customerId);
		list.addAll(bcList);
		List<Archives> beList = exaProvider.findExamForApp(customerId);
		list.addAll(beList);
		/*List<Archives> bmrList = bmrProvider.findMRForApp(customerId);
		list.addAll(bmrList);*/

		Collections.sort(list, new Comparator<Archives>() {
			public int compare(Archives arg0, Archives arg1) {
				String time1 = StringUtil.isEmpty(arg0.getRecordTime())?"":arg0.getRecordTime();
				String time2 = StringUtil.isEmpty(arg1.getRecordTime())?"":arg1.getRecordTime();
				return time2.compareTo(time1);
			}
		});
		return JsonUtil.toJson(new JsonEntity("200", list));
	}
	
	/**
	 * 健康报告
	 */
	@ResponseBody
	@RequestMapping(value = "assessment", produces = "application/json; charset=utf-8")
	public String assessment(@ModelAttribute("ba") BuzAssessment ba){
		if (ba.getCustomerId() == null) {
			return JsonUtil.toJson(new JsonEntity("-1", "参数不正确！"));
		}
		List<Map<String, Object>> baList = baProvider.findAssessmentAll(ba);
		
		return JsonUtil.toJson(new JsonEntity("200",baList));
	}
}
